<?php

declare(strict_types=1);

/**
 * @Author:     Frank
 * @contact     frank_hhm@163.com
 * @Date:       2022-11-12 11:13:20
 * @Last Modified time: 2022-12-23 16:26:23
 */
namespace app\sys\controller\system;

use app\sys\controller\Base;
use think\facade\{
    App
};
use app\common\services\system\ConfigTabService;
/**
 * 配置分类
 * Class ConfigTab 
 * @package app\sys\controller\system
 */
class ConfigTab extends Base
{
    /**
     * ConfigTab constructor.
     * @param App $app
     */
    public function __construct(App $app,ConfigTabService $service)
    {
        parent::__construct($app);
        $this->service = $service;
    }

    /**
     * 配置分类列表
     * @method(GET)
     */
    public function list()
    {
        $where = $this->request->getMore([
            ['status', ''],
            ['title', ''],
        ]);
        $this->success($this->service->getConfigTabList($where));
    }

    /**
     * 配置分类详细 
     * @noAuth(true)
     * @method(GET)
     */
    public function detail(){
        $this->success($this->service->getConfigTabDetail($this->request->get('id')));
    }

    /**
     * 添加配置分类
     * @method(POST)
     */
    public function create()
    {
        $data = $this->request->postMore([
            'title',
            'status',
            'eng_field',
            'icon',
            ['type', 0],
            ['sort', 0],
            ['pid', 0],
            ['desc','']
        ]);
        if (!$data['title']) $this->error('请输入分类名称');
        if (!$data['eng_field']) $this->error('请输入分类字段');
        if($this->service->save( $data)){
            $this->success('添加成功!');
        }
        $this->error('添加失败!');
    }

    /**
     * 编辑配置分类
     * @method(PUT)
     */
    public function update()
    {
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }
        $data = $this->request->postMore([
            'title',
            'status',
            'eng_field',
            'icon',
            ['type', 0],
            ['sort', 0],
            ['pid', 0],
            ['desc','']
        ]);
        if (!$data['title']) $this->error('请输入分类名称');
        if (!$data['eng_field']) $this->error('请输入分类字段');
        if($this->service->update($id, $data)){
            $this->success('修改成功!');
        }
        $this->error('修改失败!');
    }

    /**
     * 删除配置分类
     * @method(DELETE)
     */
    public function delete()
    {
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }

        if ($this->service->delete((int)$id)) {
            $this->success('删除成功!');
        } 
        $this->error('删除失败!');
    }

    /**
     * 修改配置分类状态
     * @method(PUT)
     */
    public function status()
    {
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }
        if ($this->service->update($id, ['status' => $this->request->param('status')])) {
            $this->success('修改成功');
        }
        $this->error('修改失败');
    }

    /**
     * 获取配置分类Tree
     * @noAuth(true)
     * @method(GET)
     */
    public function getSelectTree(){
        $pid = $this->request->get('pid',0);
        $this->success($this->service->getSelectTree($pid));
    }

}